<?php

/**
 * 渠道配置
 */

namespace App\Model\BbcModel;

use App\Model\BaseModel\BaseBbcModel;
use Service\Code\Normal;

class ConfigChannel extends BaseBbcModel {
    public $table = 'ly_config_channel';

    /**
     * 预写入的配置
     * @access public
     * @var    array
     */
    public $init_config = [
        [
            'name' => '微信扫码枪',
            'code' => 'wx_scan_gun_pay',
            'type' => 1,
        ],
        [
            'name' => '分账配置时间(T+N)',
            'code' => 'settle_bill_day',
            'type' => 2,
        ],
        [
            'name' => '充值会员',
            'code' => 'member_vip_fee',
            'type' => 3,
        ]
    ];


    /**
     * 状态
     * @access public
     * @var    array
     */
    public $status = [
        0 => '禁用',
        1 => '正常',
    ];

    /**
     * 类型
     * @access public
     * @var    array
     */
    public $type = [
        1 => '支付配置',
        2 => '分账配置',
        3 => '店铺配置',
        4 => '系统配置',
    ];

    public function getConfig(array $param = []): array {
        if (empty($param['code'])) {
            return Normal::returnCode(Normal::FAIL, 'expect param code');
        }
        if (empty($param['cid'])) {
            return Normal::returnCode(Normal::FAIL, 'expect param cid');
        }
        $data = $this->info(['code' => $param['code'], 'cid' => $param['cid'], 'status' => 1]);
        if (empty($data)) {
            return Normal::returnCode(Normal::FAIL, 'expect param exist');
        }
        $data['value'] = !empty(json_decode($data['value'])) ? json_decode($data['value'], true) : $data['value'];
        return Normal::returnCode(Normal::SUC, '获取成功', $data);
    }

    public function getPayFee($param) {
        $pay_fee_config = $this->getConfig(['cid' => $param['cid'], 'code' => 'pay_fee']);
        if (isset($pay_fee_config['data']['value']['fee']) && $pay_fee_config['data']['value']['fee'] > 0 && $pay_fee_config['data']['value']['fee'] < 1000) {
            $pay_fee = bcsub(1, bcdiv($pay_fee_config['data']['value']['fee'], 1000, 3), 3);
        } else {
            $pay_fee = 0.994;
        }
        return $pay_fee;
    }

    public function getPriceSet(array $param=[]){
        $data = $this->info(['code' => 'price_decimal', 'cid' => $param['cid'], 'status' => 1]);
        if(empty($data)){
            return 0;
        }
        if(empty($data['value'])){
            return 0;
        }else{
            $value = json_decode($data['value'], true);
            return $value['price'];
        }
    }
}
